/**
 * --------------- 基于layer的二开弹窗 ---------------
 */
define([
  "layer",
  "publicBusiness/functions"
], function(layer, Functions) {
  // 配置全局的layer样式
  layer.config({
    path: '../../lib/',
    extend: 'jwb/style.css', //加载新皮肤
  });

  var layerPc = {
    alert: function(opt){
      var opt = opt || {};
      opt.skin = opt.skin || "layer-ext-jwb";
      opt.title = opt.title || "弹窗标题";
      opt.content = opt.content || "主体内容";
      opt.area = opt.area || ["525px", "400px"];
      opt.call = opt.call || "";
      opt.btn = opt.btn || ["确定"];
      opt.end = opt.end || "";
      layer.alert(opt.content, {
        title: opt.title,
        skin: opt.skin,
        area: opt.area,
        btn: opt.btn,
        end: function(){
          opt.end && opt.end();
        }
      },function(idx){
        opt.call && opt.call();
        layer.close(idx);
      });
    },
    confirm: function(opt){
      var opt = opt || {};
      opt.skin = opt.skin || "layer-ext-jwb";
      opt.title = opt.title || "弹窗标题";
      opt.content = opt.content || "主体内容";
      opt.area = opt.area || ["525px", "400px"];
      opt.call = opt.call || "";
      opt.callSec = opt.callSec || "";
      opt.btn = opt.btn || ["确定", "取消"];
      opt.isShut = opt.isShut || false;
      opt.idx = null; //定义层的索引
      opt.idx = layer.confirm(opt.content, {
        title: opt.title,
        skin: opt.skin,
        area: opt.area,
        btn: opt.btn,
        btn2:function(idx){
          opt.callSec && opt.callSec();
          layer.close(idx);
        }
      },function(idx){
        opt.call && opt.call();
        !opt.isShut && layer.close(idx);
      });
      return opt.idx;
    },
    page: function(opt){
      var opt = opt || {};
      opt.type = 1;
      opt.skin = opt.skin || "layer-ext-jwb";
      opt.area = opt.area || ["525px", "400px"];
      opt.title = opt.title || "页面层的标题";
      opt.content = opt.content || "页面层的内容";
      layer.open(opt);
    },
    frame: function(opt){
      var opt = opt || {};
      opt.type = 2;
      opt.skin = opt.skin || "layer-ext-jwb";
      opt.area = opt.area || ["525px", "400px"];
      opt.title = opt.title || "页面层的标题";
      opt.url = opt.url;
      opt.success = opt.success || "";
      opt.call = opt.call || "";//确定按钮回调
      opt.call_cancel = opt.call_cancel || "";//取消按钮回调
      opt.not_shut = false || opt.not_shut;//确定按钮不关闭
      opt.not_shut_cancel = false || opt.not_shut_cancel;//取消按钮不关闭
      opt.isInnerParent = false || opt.isInnerParent;
      // 需要补文档
      opt.frame_id = null; //调用layer frame方法后的frame对应的id
      opt.end = opt.end || "";
      opt.cancel = opt.cancel || "";

      opt.frame_id = layer.open({
        type: opt.type,
        skin: opt.skin,
        area: opt.area,
        title: opt.title,
        content: opt.url,
        success: function(layero, index){
          opt.success && opt.success(layero, index);
          layerPc.define_frame(layero, index, opt, opt.call, opt.call_cancel);
        },
        end: function(){
          opt.end && opt.end();
        },
        cancel: function(index, layero){
          opt.cancel && opt.cancel(index, layero);
        }
      });
      return opt.frame_id;
    },
    define_frame: function(obj, index, opt, call, call_cancel){
      var opt = opt || {};
      opt.frame_yes_btn = opt.frame_yes_btn || "#frame_btn_yes";
      opt.frame_no_btn = opt.frame_no_btn || "#frame_btn_no";
      var sFrame = "layui-layer-iframe" + index;
      var oWin = !opt.isInnerParent ? Functions.getIframe(sFrame, true).win : Functions.getIframe(sFrame, true).win;
      var oDoc = !opt.isInnerParent ? Functions.getIframe(sFrame, true).doc : Functions.getIframe(sFrame, true).doc;

      // 取消按钮回调
      var oBtnNo = oDoc.find(opt.frame_no_btn);
      oBtnNo.click(function() {
        call_cancel && call_cancel(oWin, oDoc);
        if(!opt.not_shut_cancel){
          !opt.isInnerParent ? parent.layer.closeAll() : layer.closeAll();
        }
      });

      // 确定按钮回调
      var oBtnYes = oDoc.find(opt.frame_yes_btn);
      oBtnYes.click(function() {
        call && call(oWin, oDoc);
        if(!opt.not_shut){
          !opt.isInnerParent ? parent.layer.closeAll() : layer.closeAll();
        }
      });
    },
    success: function(opt){
      var opt = opt || {};
      var sTip =  opt.tip_ok_con ? opt.tip_ok_con : "操作成功";
      var sTplType = opt.tip_type ? opt.tip_type : 0;
      switch(sTplType){
        case 0:
        sTplType = "icon_suc";
        break;
        case 1:
        sTplType = "icon_warn";
        break;
        case 2:
        sTplType = "icon_failed";
        break;
        default: 

      }
      var sTpl = "<div class='success_infor'>"
              + "<i class="+ sTplType +"></i>"
              + "<p class='suc_txt'>"+ sTip +"</p>"
            + "</div>";
      opt.type = 1;
      opt.skin = opt.skin || "layer-ext-jwb";
      opt.area = opt.area || ["386px", "252px"];
      opt.time = opt.time || 2000;
      opt.title = opt.title || "提示";
      opt.content = sTpl;
      opt.call = opt.call || "";
      layer.open({
        type: opt.type,
        skin: opt.skin,
        area: opt.area,
        time: opt.time,
        title: opt.title,
        content: opt.content,
        end: function(){
          opt.call && opt.call();
        }
      });
    },
    ask: function (opt){
      var opt = opt || {};
      opt.skin = opt.skin || "layer-ext-jwb_ask";
      opt.title = opt.title || "提示";
      var sTip =  opt.tip_ask_con ? opt.tip_ask_con : "确定要删除吗？";
      var sTpl = "<div class='ask_infor'>" + sTip + "</div>";
      opt.area = opt.area || ["386px", "252px"];
      opt.call = opt.call || "";
      opt.btn = opt.btn || ["确定", "取消"];
      opt.content = sTpl;
      // ==需要补文档
      opt.not_shut = false || opt.not_shut;
      // ==end
      layer.confirm(opt.content, {
        title: opt.title,
        skin: opt.skin,
        area: opt.area,
        btn: opt.btn
      },function(idx){
        opt.call && opt.call(idx);
        if(!opt.not_shut){
          layer.close(idx);
        };
      });
    },
    msg: function (opt) { //弱提示层--限制字符 <=20
      var opt = opt || {};
      var defaults = {
        offset: "160px",
        define_type: 0, //弹窗状态 0-成功 1-失败
        contents: "操作失败",
        pointed_parent: "",
        pointed_top: 30
      }
      var config = $.extend({}, defaults, opt);
      // 定义特定皮肤
      config.skin = "msg_style_define"; //默认的最外侧layui-layer 的扩展样式，该样式很关键直接影响后面弹窗的样式

      // 给指定元素追加layer.msg dom结构
      if(config.pointed_parent){
        config.success = function(layero, index){
          $(config.pointed_parent).append("<div class='layer_msg_pointed_parent_ele' id='layer_msg_pointed_parent_ele' style='top:"+ config.pointed_top +"px;'></div>");
          $("#layer_msg_pointed_parent_ele").append(layero);
        }
      }

      var sTpl = "<p class='content_icon_zone {define_type}'>" + 
              "<span class='content_icon {define_type}'>{content}</span>" + 
            "</p>";

      if(config.define_type === 0){
        sTpl = sTpl.replace(/{define_type}/g, "success");
      }else if(config.define_type == 1){
        sTpl = sTpl.replace(/{define_type}/g, "fail");
      }
      sTpl = sTpl.replace(/{content}/g, config.contents);

      layer.msg(sTpl, config, function () {
        config.call && config.call(); //层销毁后的回调函数
      });
    },
    /**
     * [layer-tips层hover事件提示信息交互]
     * @param  {[string]} ele           [事件触发的元素-通过class名称筛选]
     * @param  {[arry]} arryAttribute [提示内容-盛放内容的属性值]
     * @param  {[object]} opt           [tips层配置信息]
     * @return {[null]}               [null]
     */
    tip: function(ele, arryAttribute, opt){ //tip提示层
      var opt = opt || {};
        var defaults = {
          disable_class: "", //禁用提示class--包含此class名称的话就不显示提示信息
          direction: 3, //提示层方向
          txt_color: "#27b2fb", //提示层的颜色
          time: 10000000, //提示层消失时间
          area: "auto", //层的宽度，默认是宽高自适应
          bind_ele: "", //事件绑定非 class 类名 例如data-role [data-role="xxx"]
          dele_ele: ""//代理元素id
        }
        var config = $.extend({}, defaults, opt);
        var aArr = arryAttribute;
        // 根据指定的绑定元素绑定事件
        if(config.bind_ele){
          if(config.dele_ele){
            $("#" + config.dele_ele).on("mouseover", config.bind_ele, function(){
              moveOver( $(this), config );
            })
            $("#" + config.dele_ele).on("mouseout", config.bind_ele, function(){
              layer.closeAll('tips');
            })
          }else{
            $(config.bind_ele).hover(function () {
              moveOver( $(this), config );
            }, function () {
              layer.closeAll('tips');
            });
          }
        }else{
          if(config.dele_ele){
            $("#" + config.dele_ele).on("mouseover", "." + ele, function(){
              moveOver( $(this), config );
            })
            $("#" + config.dele_ele).on("mouseout", "." + ele, function(){
              layer.closeAll('tips');
            })
          }else{
            $("." + ele).hover(function () {
              moveOver( $(this), config );
            }, function () {
              layer.closeAll('tips');
            });
          }
        }

        /**
         * [moveOver 鼠标移入事件]
         * @param  {[objThis]} [发生事件的当前对象]
         * @param  {[config]}  [配置对象]
         * @return {[type]} [description]
         */
        function moveOver (objThis, config) {
          // 禁用提示层
          if( config.disable_class && objThis.hasClass(config.disable_class) ){
            return;
          }
          // 拼装数据
          var sTip = "";
          for(var i = 0; i < aArr.length; i++){
            if( objThis.attr( aArr[i] ) ){
              sTip += "<p style='font-size:12px;color:#fff;text-align:left;'>"+ objThis.attr( aArr[i] ) +"</p>";
            }
          }

          var that = objThis;
          layer.tips(sTip, that, {
            tips: [config.direction, config.txt_color],
            time: config.time,
            area: config.area
          });
        }
    }
  }

  return layerPc;
});